package sdiff;

import gnu.getopt.Getopt;

import java.io.IOException;

import smallChanges.lineNumberMapping.StatementMapper;
import smallChanges.statements.StatementMappingEntry;
import smallChanges.statements.StatementMappingResultSet;

public class SDiff
{
    
    private static void usage() {
        System.err.println("Usage: java sdiff.SDiff \n" +
                "\t[ -h ] Halt with this message\n" +
                "\t[ -j <java-version>, (1.3, 1.4, 1.5), default 1.4 ]\n" +
                "\t[ -l <line matching threshold, (0 <= X <= 1.0), default 0.4> ]\n" +
                "\t[ -c <class matching threshold, (0 <= X <= 1.0), default 0.9> ]\n" +
                "\t[ -f <field matching threshold, (0 <= X <= 1.0), default 2.5> ]\n" +
                "\t[ -m <method matching threshold, (0 <= X <= 1.0), default 0.9> ]\n" +
                "\t[ -t ] Use token edit distance\n" +
                "\t[ -n ] Use min of token edit distance and character edit distance\n" +
                "<v0> <v1> ");
        System.exit(1);
    }
    
    /**
     * @param args
     */
    public static void main(String[] args)
    throws IOException
    {
        if (args.length < 2) {
            usage();
        }
        
        // Create statement mapper
        StatementMapper mapper=new StatementMapper();
        // Set to default settings
        mapper.setRunTokenEditDistance(false);
        mapper.setUseMinTokenOrTextEditDistance(false);
        mapper.setCanfora(0.4);
        mapper.setFieldMappingThreshold(2.5);
        mapper.setClassMappingThreshold(0.9);
        mapper.setMethodMappingThreshold(2.0);

        // usePossibleProbably isn't an internal setting...
        // We always compute possible and probable matches and mark
        // them as such.  You can choose to include these or not
        // as matches.
        boolean usePossibleProbable=false;
        
        String version="1.4";
        
        Getopt g = new Getopt("sdiff", args, "c:f:j:l:m:tnph");
        int c;
        while ((c = g.getopt()) != -1)
        {
            switch(c)
            {
            case 'h':
                usage();
                break;
            case 'c':
                mapper.setClassMappingThreshold(
                        Double.parseDouble(g.getOptarg()));
                break;
            case 'f':
                mapper.setFieldMappingThreshold(
                        Double.parseDouble(g.getOptarg()));
                break;
            case 'j':
                version=g.getOptarg();
                break;
            case 'l':
                mapper.setCanfora(Double.parseDouble(g.getOptarg()));
                break;
            case 'm':
                mapper.setMethodMappingThreshold(
                        Double.parseDouble(g.getOptarg()));
                break;
            case 't':
                mapper.setRunTokenEditDistance(true);
                break;
            case 'n':
                mapper.setUseMinTokenOrTextEditDistance(true);
                break;
            case 'p':
                usePossibleProbable=true;
                break;
            default:
                throw new IllegalArgumentException("getopt() returned " + c + "\n");
            }
        }

        // Remaining arguments are source files
        String leftFileStr=args[g.getOptind()];
        String rightFileStr=args[g.getOptind()+1];

        // Set source files for the mapper
        mapper.setSourceFiles(leftFileStr, version,
                rightFileStr, version);
       
        // Run the mapper
        StatementMappingResultSet rs=mapper.runStatementMapper();
        
        // find the valid matches
        mapper.setValidMappings(rs);
        // find near-miss matches, in case we're looking for them
        // These will be printed with a message making it clear
        // that they are POSSIBLE or PROBABLE matches.
        mapper.setProbablePossibleMappings(rs);

        for (StatementMappingEntry e : rs) {
            if (e.toString().length()>0) {
                System.out.println(e);
            }
        }
    }
}
